import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:honeypotflutter/modules/package/api/packageApi.dart';
import 'package:honeypotflutter/modules/package/model/package_list_model.dart';
import 'package:honeypotflutter/public/public_alert.dart';
import 'package:honeypotflutter/public/public_total.dart';
import 'package:honeypotflutter/publicBussine/bussine_colors.dart';
import 'package:honeypotflutter/publicBussine/bussine_font.dart';

class PackageListLogic extends GetxController {
  /*
  * 撤回包裹
  * */
  recallPackage({String? orderID, String? deviceName, Function()? recallAction}) async {
    if (orderID == null) {
      return;
    }
    HLAlert.showSureCancelAlert(builder: (BuildContext context, Widget child) {
      return Center(
          child: Container(
        margin: EdgeInsets.only(left: 50, right: 50),
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(8),
        ),
        width: double.infinity,
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            SizedBox(height: 16),
            Text("温馨提醒", style: TextStyle(color: MGColors.text1Color, fontSize: MGFont.titleFont4, fontWeight: FontWeight.bold)),
            Padding(
              padding: EdgeInsets.only(left: 16, right: 16, top: 8),
              child: Text.rich(TextSpan(children: [
                TextSpan(text: "警告:", style: TextStyle(color: MGColors.redColor, fontSize: MGFont.titleFont4)),
                TextSpan(text: "您正在撤回包裹,如果点击确认,机柜门立刻打开,请确保您在", style: TextStyle(color: MGColors.text1Color, fontSize: MGFont.textFont1)),
                TextSpan(text: "${deviceName}", style: TextStyle(color: MGColors.redColor, fontSize: MGFont.titleFont4)),
                TextSpan(text: "机柜旁边操作!!! 否则因此造成的损失概不负责!", style: TextStyle(color: MGColors.text1Color, fontSize: MGFont.textFont1)),
              ])),
            ),
            SizedBox(height: 10),
            Flex(direction: Axis.horizontal, children: [
              Flexible(
                  child: InkWell(
                    onTap: () {
                      Get.back();
                    },
                    child: Container(
                      padding: EdgeInsets.only(top: 15, bottom: 15),
                      width: double.infinity,
                      decoration: BoxDecoration(
                        color: Colors.white,
                        border: Border(
                          top: BorderSide(color: MGColors.splitColor, width: 1),
                          right: BorderSide(color: MGColors.splitColor, width: 1),
                        ),
                      ),
                      child: Text(
                        "取消",
                        textAlign: TextAlign.center,
                        style: TextStyle(color: MGColors.blueColor, fontSize: MGFont.textFont1, fontWeight: FontWeight.bold),
                      ),
                    ),
                  ),
                  flex: 1),
              Flexible(
                  child: InkWell(
                    onTap: () async {
                      Get.back();
                      await PackageApi.recallPackage(orderID);

                      /// 刷新列表
                      if (recallAction != null) {
                        recallAction();
                      }
                    },
                    child: Container(
                      padding: EdgeInsets.only(top: 15, bottom: 15),
                      width: double.infinity,
                      decoration: BoxDecoration(
                        color: Colors.white,
                        border: Border(
                          top: BorderSide(color: MGColors.splitColor, width: 1),
                          right: BorderSide(color: MGColors.splitColor, width: 1),
                        ),
                      ),
                      child: Text(
                        "确认",
                        textAlign: TextAlign.center,
                        style: TextStyle(color: MGColors.blueColor, fontSize: MGFont.textFont1, fontWeight: FontWeight.bold),
                      ),
                    ),
                  ),
                  flex: 1),
            ]),
          ],
        ),
      ));
    });
  }

  /*
  * 重发短信
  * */
  resend(String? orderID) async {
    if (orderID == null) {
      return;
    }
    HLAlert.showSureCancelAlert(
        title: "小蜜提醒",
        message: "是否重新发送短信?",
        sureAction: (bool isOk) async {
          if (isOk) {
            await PackageApi.resend(orderID);
            HLTotal.total("短信发送成功");
          }
        });
  }

  /*
  * 修改手机号
  * */
  modifyPhone({required PackageListItemModel model, Function()? modifySuccess}) async {
    if (model.id == null) {
      return;
    }
    HLAlert.showTextFiled(
        title: "修改手机号",
        message: "请仔细核对收件人手机号码进行修改",
        hintText: "请输入收件人手机号码",
        sureAction: (value) async {
          if (!GetUtils.isPhoneNumber(value)) {
            HLTotal.total("请输入手机号码");
            return;
          }
          await PackageApi.modifyPhone(model.id!, value);
          model.orderPhone = value;
          if (modifySuccess != null) {
            modifySuccess();
          }
        });
  }
}
